package com.nb.protocol.handle;

import com.nb.protocol.protocol.MessageHolder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 *
 *
 * @author Yohann.
 */
public class ProtocolEncoder extends MessageToByteEncoder<MessageHolder> {
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageHolder msg, ByteBuf out) throws Exception {
        /*String body = msg.getBody();

        if (body == null) {
            throw new IllegalStateException("body == null");
        }

        // 编码
        byte[] bytes = body.getBytes("utf-8");

        out.writeBytes(msg.getCpuId().getBytes())
                .writeByte(msg.getSerial())
                .writeShort(msg.getFrame())
                .writeByte(msg.getType())
                .writeBytes(bytes);*/

        out.writeBytes(msg.getRemoteAddr()).
                writeBytes(msg.getCpuIdBytes())
                .writeByte(msg.getSerial())
                .writeShort(msg.getFrame())
                .writeByte(msg.getType());
    }
}
